package api

import (
	"io"
	"net/http"
	"strconv"
	"strings"

	"gitee.com/hexug/vblog/tree/master/api/apps/post"
	"github.com/gin-gonic/gin"
)

func (a *PostApi) GetDetailPost(ctx *gin.Context) {
	id1 := ctx.Param("id")
	id, err := strconv.Atoi(id1)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  "获取参数失败。",
		})
		ctx.Abort()
		return
	}
	request := post.NewDeleteOrGetBlogRequest(id)
	b, err := a.Svc.GetDetailsBlog(ctx.Request.Context(), request)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  b,
	})
}
func (a *PostApi) GetListPosts(ctx *gin.Context) {
	page_size := ctx.Query("page_size")
	page_num := ctx.Query("page_num")
	status := ctx.Query("status")
	keywords := ctx.Query("keywords")
	request := post.NewQueryBlogRequest()
	if page_size != "" {
		size, err := strconv.Atoi(page_size)
		if err == nil {
			request.PageSize = size
		}
	}
	if page_num != "" {
		num, err := strconv.Atoi(page_num)
		if err == nil {
			request.PageNum = num
		}
	}
	if keywords != "" {
		request.Keywords = keywords
	}
	if status != "" {
		for k, v := range post.StatusMap {
			if strings.ToLower(status) == v {
				request.Status = k
			}
		}
	}
	//if err := ctx.ShouldBindJSON(request); err != nil && err != io.EOF {
	//	ctx.JSON(http.StatusBadRequest, gin.H{
	//		"Code": http.StatusBadRequest,
	//		"Msg":  err.Error(),
	//	})
	//	ctx.Abort()
	//	return
	//}
	// fmt.Println(request)
	blogs, err := a.Svc.QueryBlog(ctx.Request.Context(), request)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	// fmt.Print(blogs)
	// type A struct {
	// 	Code int64
	// 	Msg  *post.BlogsList
	// }
	// fmt.Print(sonic.Marshal(A{Code: 200,
	// 	Msg: blogs}))
	ctx.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  blogs,
	})
}
func (a *PostApi) CreatePost(ctx *gin.Context) {
	request := post.NewCreateBlogRequest()
	if err := ctx.ShouldBindJSON(request); err != nil && err != io.EOF {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	b, err := a.Svc.CreateBlog(ctx.Request.Context(), request)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  b,
	})
}
func (a *PostApi) UpdatePost(ctx *gin.Context) {
	id1 := ctx.Param("id")
	id, err := strconv.Atoi(id1)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  "获取参数失败。",
		})
		ctx.Abort()
		return
	}
	request := post.NewUpdateBlogRequest()
	if err := ctx.ShouldBindJSON(request); err != nil && err != io.EOF {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	if ctx.Request.Method == "PUT" {
		request.UpdateMod = "put"
	}
	if ctx.Request.Method == "PATCH" {
		request.UpdateMod = "patch"
	}
	request.Id = id
	b, err := a.Svc.UpdateBlog(ctx.Request.Context(), request)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  b,
	})
}
func (a *PostApi) DeletePost(ctx *gin.Context) {
	id1 := ctx.Param("id")
	id, err := strconv.Atoi(id1)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  "获取参数失败。",
		})
		ctx.Abort()
		return
	}
	request := post.NewDeleteOrGetBlogRequest(id)
	b, err := a.Svc.DeleteBlog(ctx.Request.Context(), request)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		ctx.Abort()
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  b,
	})
}
